<?php
# This is not a valid entry point to MediaWiki.
if( !defined( 'MEDIAWIKI' ) ) {
	echo <<<EOT
To install screenplay, put the following line in LocalSettings.php:
require_once( "\$IP/extensions/screenplay/screenplay.php" );
EOT;
	exit( 1 );
}

class screenplay {	
	private static $scenes = array();
	
	static public function init( &$parser ) {
	    $parser->setHook( 'script', 'screenplay::script' );
	    $parser->setHook( 'titlePage', 'screenplay::titlePage' );	
	    $parser->setHook( 'title', 'screenplay::title' );
	    $parser->setHook( 'by', 'screenplay::by' );	        
	    $parser->setHook( 'scene', 'screenplay::scene' );
	    $parser->setHook( 'action', 'screenplay::action' );
	    $parser->setHook( 'character', 'screenplay::character' );
	    $parser->setHook( 'paren', 'screenplay::paren' );
	    $parser->setHook( 'dialogue', 'screenplay::dialogue' );
	    $parser->setHook( 'transition', 'screenplay::transition' );
	    return true;
	}
 
	static public function script( $input, $args, $parser, $frame ) {
		$formatted = $parser->recursiveTagParse($input,  $frame);
		$script = '<div' . screenplay::style(" font: 12pt Courier;" . 
			screenplay::width(8.5) . " border: 1px solid black;") . '>' . $formatted . '</div>';
		return $script;
	}

	static public function titlePage( $input, $args, $parser, $frame ) {
		$formatted = $parser->recursiveTagParse($input,  $frame);
		$titlePage = '<div' . screenplay::style(" font: 12pt Courier;" . 
			screenplay::width(8.5) . screenplay::height(11) . 
			" border-bottom: 1px solid black;" .
			"page-break-before: always;") . '>' . $formatted . '</div>';
		return $titlePage;
	}	
	
	static public function title( $input, $args, $parser, $frame ) {
		$text = strtoupper(htmlspecialchars($input));
		$formatted = $parser->recursiveTagParse($text,  $frame);
		$thirdDown = array_fill(0, 17, '<br />');
		$title = implode("", $thirdDown) . '<p' . screenplay::style(" text-align: center;") . '>' . $formatted . '</p>';
		return $title;
	}
	
	static public function by( $input, $args, $parser, $frame ) {
		$text = strtoupper($input);
		$formatted = $parser->recursiveTagParse($text,  $frame);
		$title = '<p' . screenplay::style(" text-align: center;") . '>by</p>' .
			'<p' . screenplay::style(" text-align: center;") . '>' . $formatted . '</p>';
		return $title;
	}	
		
	static public function scene( $input, $args, $parser, $frame ) {
		$text = strtoupper(htmlspecialchars($input));
		$header = $text;
		#$header = '==' . $text . '==';
		array_push(screenplay::$scenes, $header);
		$formatted = $parser->recursiveTagParse($header,  $frame);
		$scene = '<br />' . '<br />' .
			'<p' . screenplay::style(screenplay::marginLeft(1.5) . 
			screenplay::marginRight(1.0)) . '>' . $formatted . '</p>';		
		return $scene;
	}
	
	static public function action( $input, $args, $parser, $frame ) {
		$text = htmlspecialchars($input);
		$formatted = $parser->recursiveTagParse($text, $frame);		
		$action = '<br />' .
			'<p' . screenplay::style(screenplay::marginLeft(1.5) . 
			screenplay::marginRight(1.0)) . '>' . $formatted . '</p>';		
		return $action;
	}	

	static public function character( $input, $args, $parser, $frame ) {
		$text = strtoupper(htmlspecialchars($input));
		$formatted = $parser->recursiveTagParse($text, $frame);
		$character = '<br />' .
			'<p' . screenplay::style(screenplay::marginLeft(3.7)) . '>' . $formatted . '</p>';		
		return $character;
	}
	
	static public function paren( $input, $args, $parser, $frame ) {
		$text = htmlspecialchars($input);
		$formatted = $parser->recursiveTagParse($text, $frame);
		$paren = 
			'<p' . screenplay::style(screenplay::marginLeft(3.1) . screenplay::marginRight(3.4)) . '>(' . $formatted . ')</p>';		
		return $paren;
	}

	static public function dialogue( $input, $args, $parser, $frame ) {
		$text = htmlspecialchars($input);
		$formatted = $parser->recursiveTagParse($text, $frame);
		$dialogue = 
			'<p' . screenplay::style(screenplay::marginLeft(2.5) . screenplay::marginRight(2.0)) . '>' . $formatted . '</p>';
		return $dialogue;
	}

	static public function transition( $input, $args, $parser, $frame ) {
		$text = strtoupper(htmlspecialchars($input));
		$formatted = $parser->recursiveTagParse($text, $frame);
		$transition = '<br />' .
			'<p' . screenplay::style(screenplay::marginLeft(6.0)) . '>' . $formatted . '</p>';		
		return $transition;
	}

	static private function style( $css ) {
		return ' style="' . $css . '"';
	}
		
	static private function width( $inches ) {
		return screenplay::createCSSInchesProperty( "width", $inches);
	}
	
	static private function height( $inches ) {
		return screenplay::createCSSInchesProperty( "height", $inches);
	}
		
	static private function marginRight( $inches ) {
		return screenplay::createCSSInchesProperty( "margin-right", $inches);
	}
		
	static private function marginLeft( $inches ) {
		return screenplay::createCSSInchesProperty( "margin-left", $inches);
	}
	
	static private function createCSSInchesProperty( $name, $inches ) {
		return " " . $name . ": " . $inches . "in;";
	}
}
?>